<?php

class Restaurants extends Admin_Controller {	
	
	function __construct()
	{		
		parent::__construct();
		
		remove_ssl();
		$this->auth->check_access('Admin', true);
		$this->lang->load('category');
		$this->load->model('Restaurant_model');
	}
	
	function index()
	{
		//we're going to use flash data and redirect() after form submissions to stop people from refreshing and duplicating submissions
		//$this->session->set_flashdata('message', 'this is our message');
		
		$data['page_title']		= 'Restoran';
		$data['restaurants']	= $this->Restaurant_model->get_restaurants_tierd();
		
		$this->load->view($this->config->item('admin_folder').'/restaurants', $data);
	}
	
	//basic category search
	function restaurant_autocomplete()
	{
		$name	= trim($this->input->post('name'));
		$limit	= $this->input->post('limit');
		
		if(empty($name))
		{
			echo json_encode(array());
		}
		else
		{
			$results	= $this->Restaurant_model->restaurant_autocomplete($name, $limit);
			
			$return		= array();
			foreach($results as $r)
			{
				$return[$r->id]	= $r->name;
			}
			echo json_encode($return);
		}
		
	}
	
	function organize($id = false)
	{
		$this->load->helper('form');
		$this->load->helper('formatting');
		
		if (!$id)
		{
			$this->session->set_flashdata('error', lang('error_must_select'));
			redirect($this->config->item('admin_folder').'/restaurants');
		}
		
		$data['restaurant']		= $this->Restaurant_model->get_restaurant($id);
		//if the category does not exist, redirect them to the category list with an error
		if (!$data['restaurant'])
		{
			$this->session->set_flashdata('error', lang('error_not_found'));
			redirect($this->config->item('admin_folder').'/restaurants');
		}
			
		$data['page_title']		= sprintf(lang('organize_category'), $data['restaurant']->name);
		
		$data['restaurant_products']	= $this->Restaurant_model->get_restaurant_products_admin($id);
		
		$this->load->view($this->config->item('admin_folder').'/organize_restaurant', $data);
	}
	
	function process_organization($id)
	{
		$products	= $this->input->post('product');
		$this->Restaurant_model->organize_contents($id, $products);
	}
	
	function form($id = false)
	{
		
		$config['upload_path']		= 'uploads/images/full';
		$config['allowed_types']	= 'gif|jpg|png';
		$config['max_size']			= $this->config->item('size_limit');
		$config['max_width']		= '1024';
		$config['max_height']		= '768';
		$config['encrypt_name']		= true;
		$this->load->library('upload', $config);
		
		
		$this->restaurant_id = $id;
		$this->load->helper('form');
		$this->load->library('form_validation');
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		
		$data['restaurants']		= $this->Restaurant_model->get_restaurants();
		$data['page_title']		= 'Form Restoran';
		
		//default values are empty if the customer is new
		$data['id']				= '';
		$data['country_id']		= 0; //HIPPO TAMBAH
		$data['zone_id']		= 0; //HIPPO TAMBAH
		$data['name']			= '';
		$data['alamat']			= ''; //TAMBAHAN
		$data['jam_buka']		= '00:00:00'; //TAMBAHAN
		$data['jam_tutup']		= '00:00:00'; //TAMBAHAN
		$data['slug']			= '';
		$data['description']	= '';
		$data['excerpt']		= '';
		$data['sequence']		= '';
		$data['image']			= '';
		$data['seo_title']		= '';
		$data['meta']			= '';
		$data['error']			= '';
		
		//create the photos array for later use
		$data['photos']		= array();
		
		if ($id)
		{	
			$restaurant		= $this->Restaurant_model->get_restaurant($id);

			//if the category does not exist, redirect them to the category list with an error
			if (!$restaurant)
			{
				$this->session->set_flashdata('error', lang('error_not_found'));
				redirect($this->config->item('admin_folder').'/restaurants');
			}
			
			//helps us with the slug generation
			$this->restaurant_name	= $this->input->post('slug', $restaurant->slug);
			
			//set values to db values
			$data['id']				= $restaurant->id;
			$data['country_id']		= $restaurant->country_id; //HIPPO TAMBAH
			$data['zone_id']		= $restaurant->zone_id; //HIPPO TAMBAHAN
			$data['name']			= $restaurant->name;
			$data['alamat']			= $restaurant->alamat; //TAMBAHAN
			$data['jam_buka']		= $restaurant->jam_buka; //TAMBAHAN //GANTIAN
			$data['jam_tutup']		= $restaurant->jam_tutup; //TAMBAHAN //GANTIAN
			$data['slug']			= $restaurant->slug;
			$data['description']	= $restaurant->description;
			$data['excerpt']		= $restaurant->excerpt;
			$data['sequence']		= $restaurant->sequence;
			$data['image']			= $restaurant->image;
			$data['seo_title']		= $restaurant->seo_title;
			$data['meta']			= $restaurant->meta;
			
			
		}
		
		$this->form_validation->set_rules('name', 'lang:name', 'trim|required|max_length[64]');
		$this->form_validation->set_rules('slug', 'lang:slug', 'trim');
		$this->form_validation->set_rules('description', 'lang:description', 'trim');
		$this->form_validation->set_rules('excerpt', 'lang:excerpt', 'trim');
		$this->form_validation->set_rules('sequence', 'lang:sequence', 'trim|integer');
		$this->form_validation->set_rules('image', 'lang:image', 'trim');
		$this->form_validation->set_rules('seo_title', 'lang:seo_title', 'trim');
		$this->form_validation->set_rules('meta', 'lang:meta', 'trim');
		
		
		// validate the form
		if ($this->form_validation->run() == FALSE)
		{
			$this->load->view($this->config->item('admin_folder').'/restaurant_form', $data);
		}
		else
		{
			
			
			$uploaded	= $this->upload->do_upload('image');
			
			if ($id)
			{
				//delete the original file if another is uploaded
				if($uploaded)
				{
					
					if($data['image'] != '')
					{
						$file = array();
						$file[] = 'uploads/images/full/'.$data['image'];
						$file[] = 'uploads/images/medium/'.$data['image'];
						$file[] = 'uploads/images/small/'.$data['image'];
						$file[] = 'uploads/images/thumbnails/'.$data['image'];
						
						foreach($file as $f)
						{
							//delete the existing file if needed
							if(file_exists($f))
							{
								unlink($f);
							}
						}
					}
				}
				
			}
			else
			{
				if(!$uploaded)
				{
					$error	= $this->upload->display_errors();
					if($error != lang('error_file_upload'))
					{
						$data['error']	.= $this->upload->display_errors();
						$this->load->view($this->config->item('admin_folder').'/restaurant_form', $data);
						return; //end script here if there is an error
					}
				}
			}
			
			if($uploaded)
			{
				$image			= $this->upload->data();
				$save['image']	= $image['file_name'];
				
				$this->load->library('image_lib');
				
				//this is the larger image
				$config['image_library'] = 'gd2';
				$config['source_image'] = 'uploads/images/full/'.$save['image'];
				$config['new_image']	= 'uploads/images/medium/'.$save['image'];
				$config['maintain_ratio'] = TRUE;
				$config['width'] = 600;
				$config['height'] = 500;
				$this->image_lib->initialize($config);
				$this->image_lib->resize();
				$this->image_lib->clear();

				//small image
				$config['image_library'] = 'gd2';
				$config['source_image'] = 'uploads/images/medium/'.$save['image'];
				$config['new_image']	= 'uploads/images/small/'.$save['image'];
				$config['maintain_ratio'] = TRUE;
				$config['width'] = 300;
				$config['height'] = 300;
				$this->image_lib->initialize($config); 
				$this->image_lib->resize();
				$this->image_lib->clear();

				//cropped thumbnail
				$config['image_library'] = 'gd2';
				$config['source_image'] = 'uploads/images/small/'.$save['image'];
				$config['new_image']	= 'uploads/images/thumbnails/'.$save['image'];
				$config['maintain_ratio'] = TRUE;
				$config['width'] = 150;
				$config['height'] = 150;
				$this->image_lib->initialize($config); 	
				$this->image_lib->resize();	
				$this->image_lib->clear();
			}
			
			$this->load->helper('text');
			
			//first check the slug field
			$slug = $this->input->post('slug');
			
			//if it's empty assign the name field
			if(empty($slug) || $slug=='')
			{
				$slug = $this->input->post('name');
			}
			
			$slug	= url_title(convert_accented_characters($slug), 'dash', TRUE);
			
			//validate the slug
			$this->load->model('Routes_model');
			if($id)
			{
				$slug	= $this->Routes_model->validate_slug($slug, $restaurant->route_id);
				$route_id	= $restaurant->route_id;
			}
			else
			{
				$slug	= $this->Routes_model->validate_slug($slug);
				
				$route['slug']	= $slug;	
				$route_id	= $this->Routes_model->save($route);
			}
			
			$j_buka=$this->input->post('jam_buka_minute'); //TAMBAHAN
			$jj_buka=$this->input->post('jam_buka_hour'); //TAMBAHAN
			$jam_buka=$jj_buka.':'.$j_buka.':00';
			
			$g_buka=$this->input->post('jam_tutup_minute');
			$gg_buka=$this->input->post('jam_tutup_hour');
			$jam_tutup=$gg_buka.':'.$g_buka.':00';
			
			$save['id']				= $id;
			$save['country_id']		= $this->input->post('country_id'); //HIPPO TAMBAHAN
			$save['zone_id']		= $this->input->post('zone_id'); //HIPPO TAMBAHAN
			$save['name']			= $this->input->post('name');
			$save['alamat']			= $this->input->post('alamat'); //TAMBAHAN
			$save['jam_buka']		= $jam_buka;//TAMBAHAN
			$save['jam_tutup']		= $jam_tutup; //TAMBAHAN
			$save['description']	= $this->input->post('description');
			$save['excerpt']		= $this->input->post('excerpt');
			$save['sequence']		= intval($this->input->post('sequence'));
			$save['seo_title']		= $this->input->post('seo_title');
			$save['meta']			= $this->input->post('meta');

			$save['route_id']		= intval($route_id);
			$save['slug']			= $slug;
			
			$restaurant_id	= $this->Restaurant_model->save($save);
			
			//save the route
			$route['id']	= $route_id;
			$route['slug']	= $slug;
			$route['route']	= 'cart/restaurant/'.$restaurant_id.'';
			
			$this->Routes_model->save($route);
			
			$this->session->set_flashdata('message', 'The restaurant has been saved!');
			
			//go back to the restaurant list
			redirect($this->config->item('admin_folder').'/restaurants');
		}
	}

	function delete($id)
	{
		
		$restaurant	= $this->Restaurant_model->get_restaurant($id);
		//if the category does not exist, redirect them to the customer list with an error
		if ($restaurant)
		{
			$this->load->model('Routes_model');
			
			$this->Routes_model->delete($restaurant->route_id);
			$this->Restaurant_model->delete($id);
			
			$this->session->set_flashdata('message', 'The restaurant has been deleted!');
			redirect($this->config->item('admin_folder').'/restaurants');
		}
		else
		{
			$this->session->set_flashdata('error', 'Restaurant not found!');
		}
	}
}